<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui"
	template="/templates/templatePrincipal.xhtml">
	<ui:param name="mbean" value="${usuarioMB}" />
	<ui:define name="fevent">
			<f:event listener="#{applicationScopeBean.preRenderView}" type="preRenderView" />
	</ui:define>
	<ui:define name="content">
		<p:outputPanel rendered="#{mbean.areaRestrita}">
			<h:form>
				<h1>Proibido acesso: área restrita!</h1>
				<p:poll interval="5" listener="#{mbean.redirecionar}" global="false" />
			</h:form>
		</p:outputPanel>

		<p:outputPanel rendered="#{mbean.fazerLogout}">
			<h:form>
				<h1>Faça o logout com o usuário logado e tente novamente!</h1>
				<p:poll interval="5" listener="#{mbean.redirecionar}" global="false" />
			</h:form>
		</p:outputPanel>

		<p:outputPanel rendered="#{mbean.jaAtivado}">
			<h:form>
				<h1>Este usuário já se encontra ativado!</h1>
				<p:poll interval="5" listener="#{mbean.redirecionar}" global="false" />
			</h:form>
		</p:outputPanel>

		<p:outputPanel rendered="#{mbean.chaveInvalida}">
			<h:form>
				<h1>Chave de ativação de usuário inválida!</h1>
				<p:poll interval="5" listener="#{mbean.redirecionar}" global="false" />
			</h:form>
		</p:outputPanel>

		<h:form id="formUsuario"
			rendered="#{mbean.ativarUsuario || mbean.exibirDados}">

			<p:toolbar>
				<p:toolbarGroup align="right">
					<p:commandButton title="#{msg.acaoSalvar}"
						value="#{msg.acaoSalvar}" onclick="confirmacaoSalvar.show();"
						icon="ui-icon-disk" type="button" />

					<p:commandButton action="#{mbean.cancelarAlteracao}"
						title="#{msg.acaoCancelar}" value="#{msg.acaoCancelar}"
						onclick="confirmacaoCancelar.show();"
						rendered="#{mbean.exibirDados}" icon="ui-icon-cancel"
						type="button" />
				</p:toolbarGroup>
			</p:toolbar>

			<p:confirmDialog message="Deseja salvar as alterações?"
				header="Confirmação" severity="alert" widgetVar="confirmacaoSalvar"
				appendToBody="true">
				<p:commandButton value="Sim" action="#{mbean.salvarCadastro}"
					update="@form" onclick="confirmacaoSalvar.hide();" />
				<p:commandButton value="Não" onclick="confirmacaoSalvar.hide(); "
					type="button" />
			</p:confirmDialog>

			<p:confirmDialog message="Deseja cancelar a alteração?"
				header="Confirmação" severity="alert"
				widgetVar="confirmacaoCancelar" appendToBody="true">
				<p:commandButton value="Sim" action="#{mbean.cancelarAlteracao}"
					update="@form" onclick="confirmacaoCancelar.hide();"
					immediate="true" />
				<p:commandButton value="Não" onclick="confirmacaoCancelar.hide(); "
					type="button" />
			</p:confirmDialog>

			<p:tabView id="abaCadastro">
				<p:tab title="Dados pessoais">
					<p:focus for="loginCadastro" />
					<h:panelGrid columns="3">
						<h:outputText value="Email (*):" />
						<p:inputText value="#{mbean.usuario.email}" size="54"
							maxlength="50" required="true" label="Email" id="emailCadastro">
							<f:validator validatorId="emailValidator" />
						</p:inputText>
						<p:message for="emailCadastro" showDetail="true"
							showSummary="true" />

						<h:outputText value="Login (*):" />
						<p:inputText value="#{mbean.usuario.login}" size="54"
							maxlength="50" required="true" label="Nome" id="loginCadastro" />
						<p:message for="loginCadastro" showDetail="true"
							showSummary="true" />


						<h:outputText value="Senha (*): "
							rendered="#{not mbean.exibirDados}" />
						<p:password value="#{mbean.password.password}" size="54"
							maxlength="50" required="true" label="Senha" feedback="false"
							id="senhaCadastro1" match="senhaCadastro2"
							rendered="#{not mbean.exibirDados}" />
						<p:message for="senhaCadastro1" showDetail="true"
							showSummary="true" rendered="#{not mbean.exibirDados}" />

						<h:outputText value="Repetir senha (*): "
							rendered="#{not mbean.exibirDados}" />
						<p:password value="#{mbean.password.password}" size="54"
							maxlength="50" required="true" label="Senha" feedback="false"
							id="senhaCadastro2" rendered="#{not mbean.exibirDados}" />
						<p:message for="senhaCadastro2" showDetail="true"
							showSummary="true" rendered="#{not mbean.exibirDados}" />


						<h:outputText value="Senha (*): "
							rendered="#{mbean.exibirDados and not mbean.alterarSenha}" />
						<p:password value="****************" size="54" maxlength="50"
							required="true" label="Senha" feedback="false" readonly="true"
							rendered="#{mbean.exibirDados and not mbean.alterarSenha}"
							redisplay="true" />
						<p:commandLink value="Alterar Senha"
							rendered="#{mbean.exibirDados and not mbean.alterarSenha}"
							update="@form" action="#{mbean.alterarSenha}" immediate="true"
							global="false" />

						<h:outputText value="Senha Atual (*): "
							rendered="#{mbean.exibirDados and mbean.alterarSenha}" />
						<p:password value="#{mbean.senhaAtual}" size="54" maxlength="50"
							required="true" label="Senha" feedback="false"
							id="senhaAtualCadastro"
							rendered="#{mbean.exibirDados and mbean.alterarSenha}" />
						<p:message for="senhaAtualCadastro" showDetail="true"
							showSummary="true"
							rendered="#{mbean.exibirDados and mbean.alterarSenha}" />

						<h:outputText value="Nova Senha (*): "
							rendered="#{mbean.exibirDados and mbean.alterarSenha}" />
						<p:password value="#{mbean.password.password}" size="54"
							maxlength="50" required="true" label="Nova Senha"
							feedback="false" id="senhaCadastro3" match="senhaCadastro4"
							rendered="#{mbean.exibirDados and mbean.alterarSenha}" />
						<p:message for="senhaCadastro3" showDetail="true"
							showSummary="true"
							rendered="#{mbean.exibirDados and mbean.alterarSenha}" />

						<h:outputText value="Repetir senha (*): "
							rendered="#{mbean.exibirDados and mbean.alterarSenha}" />
						<p:password value="#{mbean.password.password}" size="54"
							maxlength="50" required="true" label="Senha" feedback="false"
							id="senhaCadastro4"
							rendered="#{mbean.exibirDados and mbean.alterarSenha}" />
						<p:message for="senhaCadastro4" showDetail="true"
							showSummary="true"
							rendered="#{mbean.exibirDados and mbean.alterarSenha}" />


						<h:outputText value="Nome (*):" />
						<p:inputText value="#{mbean.usuario.nome}" size="54"
							maxlength="50" required="true" label="Nome" id="nomeCadastro" />
						<p:message for="nomeCadastro" showDetail="true" showSummary="true" />

						<h:outputText value="CPF:" />
						<p:inputMask value="#{mbean.usuario.cpf}" size="54" maxlength="50"
							required="false" label="CPF" mask="999.999.999-99"
							id="cpfCadastro">
							<f:validator validatorId="CPFValidator" />
							<p:ajax event="blur" update="messageCpfCadastro" global="false" />
						</p:inputMask>
						<p:message for="cpfCadastro" showDetail="true" showSummary="true"
							id="messageCpfCadastro" />

						<h:outputText value="RG:" />
						<p:inputText value="#{mbean.usuario.rg}" size="54" maxlength="50"
							required="false" label="RG" id="rgCadastro" />
						<p:message for="rgCadastro" showDetail="true" showSummary="true" />

						<h:outputText value="Sexo (*): " />
						<p:selectOneMenu value="#{mbean.usuario.sexo}" effect="drop"
							label="Sexo" required="true" id="sexoCadastro">
							<f:selectItems value="#{mbean.tiposSexo}" />
						</p:selectOneMenu>
						<p:message for="sexoCadastro" showDetail="true" showSummary="true" />

						<h:outputText value="Estado Civil: " />
						<p:selectOneMenu value="#{mbean.usuario.estadoCivil}"
							effect="drop" label="Estado Civil" required="false"
							id="estadoCivilCadastro">
							<f:selectItems value="#{mbean.estadosCivil}" var="estadoCivil"
								itemLabel="#{estadoCivil.nome}" />
						</p:selectOneMenu>
						<p:message for="estadoCivilCadastro" showDetail="true"
							showSummary="true" />

						<h:outputText value="Data Nascimento (*): " />
						<p:calendar value="#{mbean.usuario.dataNascimento}"
							showOn="button" pattern="dd/MM/yyyy" required="true"
							label="Data Nascimento" navigator="true" mindate="01/01/1900"
							maxdate="new Date()" yearRange="-100" locale="pt"
							id="dataNascimentoCadastro" />
						<p:message for="dataNascimentoCadastro" showDetail="true"
							showSummary="true" />
					</h:panelGrid>
				</p:tab>

				<p:tab title="Endereço">
					<h:panelGrid columns="3">
						<h:outputText value="Endereco:" />
						<p:inputText value="#{mbean.usuario.endereco.logradouro}"
							size="54" maxlength="50" required="false" label="Endereco"
							id="enderecoCadastro" />
						<p:message for="enderecoCadastro" showDetail="true"
							showSummary="true" />

						<h:outputText value="Número:" />
						<p:inputText value="#{mbean.usuario.endereco.numero}" size="20"
							maxlength="20" required="false" label="Número"
							id="numeroCadastro" />
						<p:message for="numeroCadastro" showDetail="true"
							showSummary="true" />

						<h:outputText value="Complemento:" />
						<p:inputText value="#{mbean.usuario.endereco.complemento}"
							size="54" maxlength="50" required="false" label="Complemento"
							id="complementoCadastro" />
						<p:message for="complementoCadastro" showDetail="true"
							showSummary="true" />

						<h:outputText value="Bairro:" />
						<p:inputText value="#{mbean.usuario.endereco.bairro}" size="54"
							maxlength="50" required="false" label="Bairro"
							id="bairroCadastro" />
						<p:message for="bairroCadastro" showDetail="true"
							showSummary="true" />

						<h:outputText value="Cidade:" />
						<p:inputText value="#{mbean.usuario.endereco.cidade}" size="54"
							maxlength="50" required="false" label="Cidade"
							id="cidadeCadastro" />
						<p:message for="cidadeCadastro" showDetail="true"
							showSummary="true" />

						<h:outputText value="Estado:" />
						<p:selectOneMenu value="#{mbean.usuario.endereco.UF}"
							effect="drop" label="UF" required="true" id="estadoCadastro">
							<f:selectItems value="#{mbean.listaEstados}" />
						</p:selectOneMenu>
						<p:message for="estadoCadastro" showDetail="true"
							showSummary="true" />

						<h:outputText value="CEP:" />
						<p:inputMask value="#{mbean.usuario.endereco.logradouro}"
							size="20" maxlength="20" required="false" label="CEP"
							mask="99999-999" id="cepCadastro" />
						<p:message for="cepCadastro" showDetail="true" showSummary="true" />
					</h:panelGrid>
				</p:tab>

				<p:tab title="Telefone">
					<p:panel header="Novo">
						<h:panelGrid columns="5">
							<h:outputText value="Tipo (*):" />
							<p:selectOneMenu value="#{mbean.telefone.tipo}" label="Tipo"
								required="false" effect="drop" id="tipoTelefone">
								<f:selectItems value="#{mbean.tiposTelefone}" var="tipo"
									itemLabel="#{tipo.nome}" />
							</p:selectOneMenu>

							<h:outputText value="Número (*):" />
							<p:inputMask value="#{mbean.telefone.numero}" size="30"
								maxlength="50" required="false" label="Número"
								mask="(99) 9999-9999" id="numeroTelefone" />

							<p:commandButton value="Adicionar"
								actionListener="#{mbean.adicionarTelefone}"
								update="gridTelefones tipoTelefone numeroTelefone messageNumeroTelefone"
								process="@this tipoTelefone numeroTelefone" />
						</h:panelGrid>
						<br />
						<p:message for="numeroTelefone" showDetail="true"
							showSummary="false" id="messageNumeroTelefone" />

					</p:panel>

					<p:panel header="Cadastrados">
						<p:dataTable var="registro" value="#{mbean.usuario.telefones}"
							emptyMessage="" id="gridTelefones">
							<p:column>
								<f:facet name="header">
									<h:outputText value="Tipo" />
								</f:facet>
								<h:outputText value="#{registro.tipo}" />
							</p:column>
							<p:column>
								<f:facet name="header">
									<h:outputText value="Número" />
								</f:facet>
								<h:outputText value="#{registro.numero}" />
							</p:column>
							<p:column style="width:32px">
								<p:commandButton image="ui-icon-trash"
									action="#{mbean.excluirTelefone(registro)}" immediate="true"
									oncomplete="atualizarGrid();" />
							</p:column>
						</p:dataTable>
					</p:panel>

					<p:remoteCommand update="gridTelefones" name="atualizarGrid" />
				</p:tab>
			</p:tabView>

			<p:growl showSummary="true" showDetail="true" globalOnly="true" />
		</h:form>
	</ui:define>
</ui:composition>

